﻿using CC.Core;
using CC.Model;
using System.Data;

namespace CC.Data
{
    public class Repository: IRepository
    { 
 
        public T? QueryEntity<T>(long id) where T : class
        {
            var sql = $"select * from {typeof(T).Name} where id={id}";
            return DBHelper.Query<T>(CommandType.Text, sql).FirstOrDefault();
        }

        public T? QueryEntity<T>(string sql) where T : class
        { 
            return DBHelper.Query<T>(CommandType.Text, sql).FirstOrDefault();
        }

        public List<T> Query<T>(string sql) where T : class
        {
            return DBHelper.Query<T>(CommandType.Text, sql);
        }

        /// <summary>
        /// 修改登录状态
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="userId"></param>
        /// <param name="isLogin"></param>
        /// <returns></returns>
        public bool UpadteLoginState<T>(int userId,bool isLogin) where T : UserBase
        {
            string tableName =typeof(T).Name;

            int loginState = isLogin ? 1 : 0;

            string sql = $"update [{tableName}s] set LoginState={loginState} where Id={userId}";

            return DBHelper.ExecuteNonQuery(sql) > 0;
        }
    }
}
